// 全局自定义指令
import Vue from 'vue'
import employeesData from '@/api/constant/employees'
function fn(dom, obj, vnode) {
  const findItem = employeesData[obj.value.arg].find(item => {
    return +item.id === +obj.value.val
  })
  dom.innerText = findItem ? findItem.value : '-'
}
let outFn = () => {}
const dirs = {
  xxx: {},
  aaa: {},
  bbb: {},
  errorimg: {
    inserted(dom, obj, vnode) {
      dom.src = dom.src || obj.value || require('@/assets/common/head.jpg')
      dom.onerror = () => {
        dom.src = obj.value || require('@/assets/common/head.jpg')
      }
    },
    componentUpdated(dom, obj) {
      dom.src = dom.src || obj.value || require('@/assets/common/head.jpg')
    }
  },
  // 数据转换自定义指令
  changedata: {
    inserted(dom, obj, vnode) {
      // dom:指令所在dom
      // obj:{value值,属性，修饰符}
      // vnode:{context:指令所在组件的this}
      fn(dom, obj, vnode)
    },
    componentUpdated(dom, obj, vnode) {
      fn(dom, obj, vnode)
    }
  },
  // 点击在指令所在dom外时执行一个事件
  clickout: {
    inserted(dom, obj) {
      outFn = (e) => {
        console.log('xxxxx:clickout')
        if (!dom.contains(e.target)) {
          obj.value()
        }
      }
      document.addEventListener('click', outFn)
    },
    unbind() {
      document.removeEventListener('click', outFn)
    }
  }
}
// for (const key in dirs) {
//   Vue.directive(key, dirs[key])
// }
// Object.keys(dirs).forEach(key => {
//   Vue.directive(key, dirs[key])
// })
Object.entries(dirs).forEach(item => {
  Vue.directive(item[0], item[1])
})

// 循环对象：将对象转换成数组
/*
  obj={a:1,b:2}
  Object.keys(obj) ==== ['a','b']
  Object.values(obj)=== [1,2]
  Object.entries(obj)===  [['a',1],['b',2]]
*/

